package edu.gdin.ecommerce.service.impl;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import edu.gdin.ecommerce.dao.ParamDetailDAO;
import edu.gdin.ecommerce.dao.ParamTypeDAO;
import edu.gdin.ecommerce.model.ParamDetail;
import edu.gdin.ecommerce.model.ParamType;
import edu.gdin.ecommerce.service.IParamService;
import edu.gdin.ecommerce.util.Pager;

@Service("paramService")
public class ParamService implements IParamService {
	
	private ParamTypeDAO paramTypeDAO;
	private ParamDetailDAO paramDetailDAO;
	
	public ParamTypeDAO getParamTypeDAO() {
		return paramTypeDAO;
	}
	
	public ParamDetailDAO getParamDetailDAO() {
		return paramDetailDAO;
	}
	
	@Resource(name="paramDetailDAO")
	public void setParamDetailDAO(ParamDetailDAO paramDetailDAO) {
		this.paramDetailDAO = paramDetailDAO;
	}

	@Resource(name="paramTypeDAO")
	public void setParamTypeDAO(ParamTypeDAO paramTypeDAO) {
		this.paramTypeDAO = paramTypeDAO;
	}
	
	public Pager queryParamType(String name, String desc, int currentPage){
		Pager pager = new Pager();
		pager = paramTypeDAO.queryParamTypeByNameOrDesc(name, desc, 
				currentPage);
		
		return pager;
	}

	public boolean addParamType(ParamType type) {
		if (!paramTypeDAO.isExist(ParamType.class, "name", type.getName()))
			return paramTypeDAO.saveParamType(type);
		else
			return false;
	}

	/**
	 * 查询系统参数
	 */
	public Pager queryParamDetail(ParamDetail pDetail, int cPage) {
		
		Pager pager = new Pager();
		
		pager = paramDetailDAO.queryParamDetail(pDetail , cPage);
		
		return pager;
	}
	
	public List<ParamType> queryAllTypes(){
		return (List<ParamType>) paramTypeDAO.queryAllTypes();
	}

//	@Override
	public ParamType queryParamTypeById(Integer id) {
		return (ParamType) paramTypeDAO.findByPk(ParamType.class, id);
	}

//	@Override
	public boolean addParamDetail(ParamDetail pd) {
		try{
			paramDetailDAO.insert(pd);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<ParamDetail> getAllParamDetails() {
		return (List<ParamDetail>) this.paramDetailDAO.list(ParamDetail.class);
	}
	
	public Pager queryParamDetailByParamTypeId(Integer paramTypeId, Integer cPage){
		return this.paramDetailDAO.queryParamDetailByParamTypeId(paramTypeId, cPage);
	}
}
